#include "BarButton.h"

CBrButton::CBrButton() {
    m_bShow = true;
    m_bListen = true;
    m_status = BR_BUTTON_STATUS_OUT;
    m_func_pressed = NULL;
    m_iId = 0;
    m_rcButton.x = 0;
    m_rcButton.y = 0;
    m_rcButton.w = 0;
    m_rcButton.h = 0;
    m_pTextureIn = NULL;
    m_pTextureOut = NULL;
    m_pTexturePressed = NULL;
}

CBrButton::~CBrButton() {
    if (NULL != m_pTextureIn)
    {
        SDL_DestroyTexture(m_pTextureIn);
    }
    if (NULL != m_pTextureOut)
    {
        SDL_DestroyTexture(m_pTextureOut);
    }
    if (NULL != m_pTexturePressed)
    {
        SDL_DestroyTexture(m_pTexturePressed);
    }

}
void CBrButton::setFuncPressed(void (*func_pressed)(const CBrButton *)) {
    m_func_pressed = func_pressed;
}
void CBrButton::CreateTexture(SDL_Surface* surfaceIn, SDL_Surface* surfaceOut,
                              SDL_Surface* surfacePressed) {
    if (NULL != m_renderer)
    {
        if (NULL == m_pTextureIn)
        {
            m_pTextureIn = SDL_CreateTextureFromSurface(m_renderer, surfaceIn);
        }
        if (NULL == m_pTextureOut)
        {
            m_pTextureOut = SDL_CreateTextureFromSurface(m_renderer, surfaceOut);
        }
        if (NULL == m_pTexturePressed)
        {
            m_pTexturePressed = SDL_CreateTextureFromSurface(m_renderer, surfacePressed);
        }
    }
}
bool CBrButton::bPointIn(int x, int y) {
    if (x >= m_rcButton.x && x <= (m_rcButton.x+m_rcButton.w) && y >= m_rcButton.y && y <= (m_rcButton.y+m_rcButton.h))
    {
        return true;
    }
    else
    {
        return false;
    }

}
bool CBrButton::handleEvent(SDL_Event & stEvent) {
    if (!m_bListen) {
        return false;
    }

    bool bRet = false;
    if (stEvent.type == SDL_MOUSEMOTION) {
        if (m_status == BR_BUTTON_STATUS_IN
            || m_status == BR_BUTTON_STATUS_OUT) {
            if (bPointIn(stEvent.motion.x, stEvent.motion.y)) {
                m_status = BR_BUTTON_STATUS_IN;
                bRet = true;
            } else {
                m_status = BR_BUTTON_STATUS_OUT;
            }
        } else if (m_status == BR_BUTTON_STATUS_PRESSED) {
            if (!bPointIn(stEvent.motion.x, stEvent.motion.y)) {
                m_status = BR_BUTTON_STATUS_OUT;
            }
        }

    } else if (stEvent.type == SDL_MOUSEBUTTONDOWN) {
        if (bPointIn(stEvent.button.x, stEvent.button.y)) {
            m_status = BR_BUTTON_STATUS_PRESSED;
            bRet = true;
        }
    } else if (stEvent.type == SDL_MOUSEBUTTONUP) {
        BrButtonStatus oldStatus = m_status;
        if (bPointIn(stEvent.button.x, stEvent.button.y)) {
            m_status = BR_BUTTON_STATUS_IN;
            bRet = true;
        } else {
            m_status = BR_BUTTON_STATUS_OUT;
        }
        if (oldStatus == BR_BUTTON_STATUS_PRESSED) {
            if (m_func_pressed != NULL) {
                m_func_pressed(this);
                bRet = true;
            }
        }
    }
    return bRet;
}

SDL_Texture * CBrButton::GetShowTexture() {
    if (m_status == BR_BUTTON_STATUS_IN) {
        return m_pTextureIn;
    } else if (m_status == BR_BUTTON_STATUS_PRESSED) {
        return m_pTexturePressed;
    } else {
        return m_pTextureOut;
    }
}

void CBrButton::SetRect(const SDL_Rect &rc)
{
    m_rcButton = rc;
}

SDL_Rect CBrButton::GetRect()
{
    return m_rcButton;
}


void CBrButton::render()
{
    SDL_Texture* textText = GetShowTexture();
    if (NULL != m_renderer && NULL != textText)
    { printf("button render  ");
        SDL_RenderCopy(m_renderer, textText, NULL, &m_rcButton);
    }
}